<?php
declare (strict_types = 1);

namespace app\controller;

use app\BaseController;
use app\model\Admin;
use app\service\AdminService;
use app\service\AuthService;
use app\validate\UserValidate;
use think\Request;
use think\App;

class AdminController extends BaseController
{
    private $service;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->service = new AdminService();
    }

    public function getInformation() {
        $result = $this->service->getInformation();
        return $this->returnJson($result);
    }
    public function delAdmin(Admin $id) {
        $this->service->deleteAdmin($id->id);
        return $this->returnJson();
    }

    public function addAdmin(){
        $data = $this->request->param(['username','email','password','is_admin']);
        validate(UserValidate::class)
            ->scene('addAdmin')
            ->check($data);
        $result = $this->service->craeteAdmin($data['username'],$data['password'],$data['email'],$data['is_admin']);
        if ($result != null) return $this->returnJson($result);
    }

    public function setAdmin(Admin $id){
        $data = $this->request->param(['username','password','email','is_admin']);
        validate(UserValidate::class)
            ->scene('setAdmin')
            ->check($data);
        $result = $this->service->setAdmin($id->id,$data['username'],$data['password'],$data['email'],$data['is_admin']);
        return $this->returnJson($result);
    }
}
